<template>
  <div>
    <el-dialog :title="user.isAdd?'活动添加':'活动修改'" :visible.sync="user.isshow" @closed="close">
      <el-form :model="form">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动期限" label-width="120px">
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            @change="reqseckillChange"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="form.first_cateid" @change="firstChange" placeholder="请选择">
            <el-option label="请选择" disabled value="shanghai"></el-option>
            <!-- 这里的value是id  因为接收到的角色的list中只有id -->
            <el-option
              v-for="item in catelist"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="form.second_cateid" @change="secondChange" placeholder="请选择">
            <el-option label="请选择" disabled value="shanghai"></el-option>
            <!-- 这里的value是id  因为接收到的角色的list中只有id -->
            <el-option
              v-for="item in seondCateId"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-select v-model="form.goodsid" placeholder="请选择">
            <el-option label="请选择" disabled :value="0"></el-option>
            <!-- 这里的value是id  因为接收到的角色的list中只有id -->
            <el-option
              v-for="item in goods"
              :key="item.id"
              :label="item.goodsname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="user.isshow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="user.isAdd">确 定</el-button>
        <el-button type="primary" @click="edit" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { reqrolelist, reqroleinfo } from "../../request/api";
import {
  reqseckilledit,
  reqseckillinfo,
  reqgoodslist,
  reqseckilladd,
  reqcatelist,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  mounted() {
    if (this.catelist.length === 0) {
      this.reqcatelist();
    }

    this.reqgoodsList();
  },
  computed: {
    ...mapGetters({
      info: "user",
      catelist: "shopCate/list",
      goodlist: "goods/list",
    }),
  },
  methods: {
    ...mapActions({
      changeUser: "reqlogin",
      reqcatelist: "shopCate/reqList",
      reqgoodsList: "goods/reqList",
      reqseckillList: "seckill/reqList",
      // reqseckillChange: "seckill/reqseckillChange",
    }),
    reqseckillChange(time) {
      let time1 = new Date(time[0]).getTime();
      let time2 = new Date(time[1]).getTime();
      // commit('endseckill', time)
      // console.log(time1, time2);
      this.form.begintime = time1;
      this.form.endtime = time2;
    },

    // 根据一级列表获取二级列表
    firstChange() {
      // 清空二级菜单
      this.form.second_cateid = "";
      // 清空商品
      this.goods = [];
      this.form.goodsid = "";
      this.getSecondsArr();
    },

    getSecondsArr() {
      reqcatelist({
        pid: this.form.first_cateid,
      }).then((res) => {
        if (res.data.code == 200) {
          console.log(res);
          this.seondCateId = res.data.list;
        }
      });
    },

    // 获取商品
    secondChange() {
      this.goods = [];
      // console.log(this.goods);
      this.getGoods();

      // let arr = [];
      // this.goodlist.forEach((item) => {
      //   if (item.first_cateid == this.form.first_cateid) {
      //     arr.push(item);
      //   }
      // });4292120001124433
      // this.goods = arr;
    },

    getGoods() {
      reqgoodslist({
        fid: this.form.first_cateid,
        sid: this.form.second_cateid,
      }).then((res) => {
        this.goods = res.data.list;
      });
    },

    //   点击空白处判断是否清空
    close() {
      console.log(this.user);
      if (!this.user.isAdd) {
        this.empty();
      }
    },

    //   修改数据
    edit() {
      reqseckilledit(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          // 清空数据
          this.empty();
          //   刷新数据
          this.reqseckillList();
          //   弹框消失
          this.cancle();
        }
      });
    },

    // 修改为标准时间格式
    getMyDate(str) {
      var date = new Date(str),
        oYear = date.getFullYear(), //年
        oMonth = date.getMonth() + 1, //月
        oDay = date.getDate(), //日
        h = date.getHours(), //小时
        m = date.getMinutes(), //分钟
        s = date.getSeconds(); //秒数

      // 以自己需要的方式拼接
      var oTime =
        oYear +
        "-" +
        (oMonth + "").padStart(2, "0") +
        "-" +
        (oDay + "").padStart(2, "0") +
        " " +
        (h + "").padStart(2, "0") +
        ":" +
        (m + "").padStart(2, "0") +
        ":" +
        (s + "").padStart(2, "0");
      //最后拼接时间
      return oTime;
    },
    //   拿到一条数据
    getOne(id) {
      reqseckillinfo({
        id: id,
      }).then((res) => {
        if (res.data.code === 200) {
          let timeArr = [];
          this.form = res.data.list;
          this.form.id = id;
          // let val1 = this.getMyDate(Number(this.form.begintime));
          // let val2 = this.getMyDate(Number(this.form.endtime));
          let val1 = new Date(Number(this.form.begintime));
          let val2 = new Date(Number(this.form.endtime));
          timeArr.splice(0, 0, val1, val2);
          this.value2 = timeArr;
          // console.log(this.value2);
          // 再次请求二级分类  和商品
          this.getSecondsArr();
          this.getGoods();
        }
      });
    },

    add() {
      reqseckilladd(this.form).then((res) => {
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          //   console.log(this.form);
          this.reqseckillList();
          // form消失
          this.cancle();
          // 清空form
          this.empty();
          //   刷新列表
        }
      });
    },

    cancle() {
      this.user.isshow = false;
    },

    // 清空数据
    empty() {
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      this.value2 = "";
    },
  },
  props: ["user"],
  data() {
    return {
      list: [],
      firstArr: [],
      seondCateId: [],
      goods: [],
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value2: "",
    };
  },
};
</script>

<style scoped>
.el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner,
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto;
}
</style>